package com.example.Blog.dao;

import com.example.Blog.model.Article;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface ArticleDao extends JpaRepository<Article, Integer> {
    Article findByArticleId(int articleId);

    Page<Article> findAllByArticleType(Pageable pageable, String articleType);

    Page<Article> findByArticleBelongTo(Pageable pageable, int userId);

    @Modifying
    @Transactional
    void deleteByArticleId(int articleId);

    @Modifying
    @Query("update Article article set article.articleLikes =: articleLikes, article.articleStars =: articleStars where article.articleId =: articleId")
    void updateByArticleId(@Param("articleLikes") int articleLikes, @Param("articleStars") int articleStars, @Param("articleId") int articleId);

    @Modifying
    @Query("update Article article set article.articleContent =: articleContent where article.articleId =: articleId")
    void updateByArticleId(@Param("articleContent") String articleContent, @Param("articleId") int articleId);
}
